IF EXISTS (SELECT * FROM SYSOBJECTS WHERE name='fnGetErrorNumber' and XTYPE='FN')
BEGIN
	DROP FUNCTION [dbo].[fnGetErrorNumber]
END
GO
CREATE FUNCTION fnGetErrorNumber
(
	 @ERROR_SEVERITY INT
	,@ERROR_STATE INT
	,@ERROR_PROCEDURE VARCHAR(128)
	,@ERROR_LINE INT
)
RETURNS INT
WITH ENCRYPTION
AS
BEGIN
	DECLARE @position INT
	DECLARE @RETURN INT
	DECLARE @AUX VARCHAR(2048)
	
	SET @position=1	
	SET @AUX=@ERROR_PROCEDURE--+@ERROR_MESSAGE
	set @RETURN=(@ERROR_SEVERITY*2)+(@ERROR_STATE*3)+(@ERROR_LINE*4)

	WHILE @position <= DATALENGTH(@AUX)
	BEGIN
		SET @RETURN=@RETURN+ASCII(SUBSTRING(@AUX, @position, 1))
		SET @position = @position + 1
	END

	RETURN @RETURN
END
GO
